package com.tcl.logoidentify.redis;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.ArrayUtils;

import redis.clients.jedis.Jedis;

import com.tcl.logoidentify.commons.ByteUtils;
import com.tcl.logoidentify.commons.IPv4Utils;

public class FPPub {
	private static final String url = "http://172.31.4.65:8080/web-api/api/v1/logoidentify/search?app_name=com.tcl.tv&inputsource_type=dtv&channel_name=cctv1&user_id=cuijian&create_times=100&client_fingerprint=";
	private static final short[] CH_IDS = new short[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
			234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 301, 302, 303, 304 };
	private static Random random = new Random();
	private static long n = 0;

	public static void main(String[] args) throws InterruptedException, UnsupportedEncodingException {
		Jedis jedis = new Jedis("54.223.157.141", 6379);
		while (n++ < Long.MAX_VALUE) {
			TimeUnit.MILLISECONDS.sleep(1);
			byte[] code = genCode();
			jedis.publish("all_chs".getBytes(), code);
		}
	}

	private static byte[] genCode() throws UnsupportedEncodingException {
		byte[] fp = new byte[11];
		byte style = 1;
		short ch_id = CH_IDS[random.nextInt(CH_IDS.length)];
		short time_len = (short) random.nextInt(5);
		random.nextBytes(fp);
		if ((n % 1000 == 0)) {
			String base64 = Base64.encodeBase64String(ArrayUtils.addAll(fp, fp));
			System.out.println(url+URLEncoder.encode(base64, "utf-8"));
		}
		byte[] fingercode = ArrayUtils.addAll(ArrayUtils.addAll(ArrayUtils.addAll(fp, style), ByteUtils.short2Byte(ch_id)), ByteUtils.short2Byte(time_len));
		String clientIp = "192.168.0.1";
		long timestamp = System.currentTimeMillis();
		byte[] bytes_ip = IPv4Utils.ipToBytesByReg(clientIp);
		byte[] bytes_timestamp = ByteUtils.long2Byte(timestamp);
		byte[] code = ArrayUtils.addAll(ArrayUtils.addAll(fingercode, bytes_ip), bytes_timestamp);
		return code;
	}

}
